package cn.tedu.zblogs.home.utils;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.time.Duration;
import java.util.concurrent.TimeUnit;

/**
 * Caffeine内存的缓存工具类
 */

@Component
public class CaffeineUtil {


    // 创建缓存对象
    Cache<String, Serializable> cache = Caffeine.newBuilder()
            // 设置缓存有效期为 10 秒，从最后一次写入开始计时
            .expireAfterWrite(Duration.ofSeconds(60))
            .build();
    ;


    //存数据
    public  void set(String key , Serializable value) {

        cache.put(key, value);
    }

    //取数据
    public  Serializable get(String key) {
        return  cache.getIfPresent(key);

    }
}
